package com.linkhub.oms.entity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldNameConstants;

import static com.linkhub.oms.entity.resulttype.stock.ProductStockResultEntity.DEFAULT_STOCK_COUNT;

/**
 * <p>
 * 本地商品库存表
 * </p>
 *
 * @author echo
 * @since 2024-04-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_product_stock")
@ApiModel(value="ProductStockEntity对象", description="本地商品库存表")
@FieldNameConstants
public class ProductStockEntity implements Serializable {

    public static final String STASH_NAME = "微仓";


    @ApiModelProperty(value = "主键")
    @TableId(value = "Fid", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "用户id")
    @TableField("Fuser_id")
    private String userId;

    @ApiModelProperty(value = "本地商品skuId")
    @TableField("Fsku_id")
    private String skuId;

    @ApiModelProperty(value = "仓库名称")
    @TableField("Fstash_name")
    private String stashName;

    @ApiModelProperty(value = "实物库存")
    @TableField("Fphysical_stock")
    private Integer physicalStock;

    @ApiModelProperty(value = "可用库存")
    @TableField("Favailable_stock")
    private Integer availableStock;

    @ApiModelProperty(value = "预占库存")
    @TableField("Fpreemption_stock")
    private Integer preemptionStock;

    @ApiModelProperty(value = "冻结库存")
    @TableField("Ffrozen_stock")
    private Integer frozenStock;

    @ApiModelProperty(value = "在途库存")
    @TableField("Fpassage_stock")
    private Integer passageStock;

    @ApiModelProperty(value = "正品数量")
    @TableField("Fgood_stock")
    private Integer goodStock;

    @ApiModelProperty(value = "次品数量")
    @TableField("Fbad_stock")
    private Integer badStock;

    @ApiModelProperty(value = "创建时间")
    @TableField("Fcreated_time")
    private LocalDateTime createdTime;

    @ApiModelProperty(value = "修改时间")
    @TableField("Fupdate_time")
    private LocalDateTime updateTime;


    public Integer computeAvailableStock() {
        int availableStock = ObjectUtil.defaultIfNull(this.getPhysicalStock(), DEFAULT_STOCK_COUNT)
                - ObjectUtil.defaultIfNull(this.getPreemptionStock(), DEFAULT_STOCK_COUNT)
                - ObjectUtil.defaultIfNull(this.getFrozenStock(), DEFAULT_STOCK_COUNT);

        return availableStock;
    }

    public static ProductStockEntity createDefaultStock(String userId, String skuId, String stashName) {
        ProductStockEntity productStockEntity = new ProductStockEntity();
        productStockEntity.setUserId(userId);
        productStockEntity.setSkuId(skuId);
        productStockEntity.setStashName(stashName);
        productStockEntity.setPhysicalStock(0);
        productStockEntity.setPreemptionStock(0);
        productStockEntity.setFrozenStock(0);
        productStockEntity.setPassageStock(0);
        productStockEntity.setAvailableStock(0);
        productStockEntity.setGoodStock(0);
        productStockEntity.setBadStock(0);
        return productStockEntity;

    }

}
